<template>
  <div class="searchTea">
    <div>
      <el-input
        style="margin-right: 30px"
        placeholder="请输入内容"
        prefix-icon="el-icon-search"
        v-model="teaName"
        @input="debounceGetData"
        @keyup.native="handleKeyUp"
      >
      </el-input>
      <el-button @click="getSearchResult" type="success" icon="el-icon-search"
        >搜索</el-button
      >
    </div>
  </div>
</template>

<script>
// import { getRequest } from "../../util/request";
export default {
  name: "search",
  data() {
    return {
      teaName: "",
    };
  },
  props: ["searchData", "isInit"],
  methods: {
    search(value) {
      this.$emit("gotoInit", true);
      setTimeout(() => {
        const { tableData } = this.searchData;
        let result = tableData.filter((item) => {
          return (
            item?.username?.includes(value.trim()) ||
            item?.name?.includes(value.trim())
          );
        });
        this.$emit("selData", result);
      }, 50);
    },
    getSearchResult() {
      this.$emit("gotoInit", true);
      setTimeout(() => {
        const { tableData } = this.searchData;
        let result = tableData.filter((item) => {
          return (
            item?.username?.includes(this.teaName.trim()) ||
            item?.name?.includes(this.teaName.trim())
          );
        });
        this.$emit("selData", result);
      }, 50);
    },

    debounceGetData(value) {
      this.$emit("gotoInit", true);
      setTimeout(() => {
        const { tableData } = this.searchData;
        let result = tableData.filter((item) => {
          return (
            item?.username?.includes(value.trim()) ||
            item?.name?.includes(value.trim())
          );
        });
        this.$emit("selData", result);
      }, 50);
    },

    handleKeyUp(event) {
      if (event.keyCode != 13) {
        return;
      }
      this.$emit("gotoInit", true);
      setTimeout(() => {
        const { tableData } = this.searchData;
        let result = tableData.filter((item) => {
          return (
            item?.username?.includes(event.target.value.trim()) ||
            item?.name?.includes(event.target.value.trim())
          );
        });
        this.$emit("selData", result);
      }, 50);
    },
  },
  mounted() {},

  beforeDestroy() {},
};
</script>

<style scoped lang='less'>
.searchTea {
  width: 100%;
  height: 40px;
  line-height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 20px auto;
  & > div {
    width: 400px;
    height: 40px;
    display: flex;
  }
}
</style>